package studentStatus.view;

import studentStatus.constants.Sqls;
import studentStatus.tools.DatasourceUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

public class TableWindow extends JFrame implements Sqls {
    private static TableWindow instance;
    private JTable table;
    private JButton closeButton;

    public static TableWindow getInstance() {
        if (instance == null) {
            synchronized (TableWindow.class) {
                if (instance == null) {
                    instance = new TableWindow();
                }
            }
        }
        return instance;
    }

    private TableWindow() {
        // 设置窗口标题
        setTitle("留言列表");
        setAlwaysOnTop(true);

        // 创建表格数据
        Object[][] data;
        try {
            data = (Object[][]) DatasourceUtils.getSetArrays(DatasourceUtils.query(sql6_mess_list, new Object[]{}));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        // 创建表格的列名
        String[] columnNames = {"序号", "姓名","内容","电话"};

        // 创建表格
        table = new JTable(data, columnNames);

        // 创建关闭按钮
        closeButton = new JButton("关闭");

        // 设置布局管理器
        setLayout(new BorderLayout());

        // 将表格添加到窗口的中间位置
        add(new JScrollPane(table), BorderLayout.CENTER);

        // 将关闭按钮添加到窗口的底部
        add(closeButton, BorderLayout.SOUTH);

        // 添加关闭按钮的点击事件处理
        closeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose(); // 关闭窗口
            }
        });

        // 设置窗口大小和可见性
        setSize(400, 300);
        setLocationRelativeTo(null); // 将窗口居中显示
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

}
